<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/12/1
 * Time: 下午8:39
 */

class BaseController extends Controller {
    public $appId, $controlId, $actionId, $url, $current;
    public $isAuth;

    public function beforeAction($action){
        $this->appId     = Yii::app()->getId();
        $this->controlId = $this->getId();
        $this->actionId  = $action->getId();
        $this->url       = PubFunc::getUrl($this->appId, $this->controlId, $this->actionId);

        if(empty($this->current['Route'])) $this->current['Route']  = $this->R('r');
        if(empty($this->current['MenuID'])) $this->current['MenuID'] = $this->R('m');

        $this->checkAuth();
        $this->checkDebug();

        $this->out['beforeAction'] = array(
            'Current'   => $this->current,
            'User'      => PubAuth::getUser(),
        );
        
        $server_name = $_SERVER['SERVER_NAME'];
        $webName = '内测站点';
        if($server_name == 'admin.s10.q-dazzle.com'){
        	$webName = '正式站点';
        }
        if($server_name == '120.78.80.94'){
        	$webName = '外测站点';
        }
        $this->out['webName'] = $webName;

        return parent::beforeAction($action);
    }

    //在渲染前检查是否在app目录存在layout文件，如果不存在则切换layout目录
    public function beforeRender($view){
        if(!empty($this->layout) && !$this->getLayoutFile($this->layout)){
            Yii::app()->setLayoutPath(Yii::getPathOfAlias ('docroot').'/common/protected/layouts/');
        }
        return parent::beforeRender($view);
    }

    //渲染之后，保存翻译文档
    protected function afterRender($view, &$output)
    {
        return parent::afterRender($view, $output);
    }

    // 通用异常处理
    public function catchError(BaseException $e){
        if (YII_DEBUG){
            throw $e;
        }else{
            if(Yii::app()->request->getIsAjaxRequest() || $_REQUEST['getListForAjax'] == 1){
                echo '{"sEcho":1,"iTotalRecords":0,"iTotalDisplayRecords":0,"sColumns":"platform,engine,browser,grade,version","aaData":[]}';
                exit();
            }else{
                $this->redirect('/home/?r=page/disable');
            }
        }
    }

    // 设置标题的函数
    public function setTitle($main, $sub=''){
        $this->out['beforeAction']['Title'] = $main;
        if(!empty($sub)) $this->out['beforeAction']['SubTitle'] = $sub;
    }

    // 通用跳转方法
    public function showMessage($message, $urlForward='', $second=1){

        $this->out = array(
            'message'   => $message,
            'url'       => $urlForward,
            'second'    => $second
        );

        $this->layout = '';

        $this->tpl = "showMessage.tpl";

        //将ViewPath设置到Common目录下
        Yii::app()->setViewPath(Yii::getPathOfAlias('docroot').'/common/protected/views');
        
    }

    // 如果debug模式则显示WebLog
    protected function checkDebug(){
        if(YII_DEBUG && $this->R('debug')){
            $route = Yii::createComponent(array(
                'class'=>'CWebLogRoute',
                'levels'=>'trace, info, error, profile',
            ));
            $route->init();

            Yii::app()->log->setRoutes(array(
                'web' => $route
            ));
        }
    }

    public function err_page($message=";-( 出错啦！"){

        $this->layout = 'normal';

        Yii::app()->setViewPath(Yii::getPathOfAlias('docroot').'/common/protected/views');

        $this->out['err_message'] = $message;
        $this->tpl = 'err_page.tpl';

    }

    // 检测权限
    public function checkAuth(){
        $normalUrl  = Yii::app()->params['NormalUrl'];
        $loginUrl   = Yii::app()->params['LoginUrl'];
        $config     = Yii::app()->params['AccAppConfig'];
        if(in_array($this->url, $normalUrl)){
            $a = PubAuth::run($config, false, false);
        }else if(in_array($this->url, $loginUrl)){
            $a = PubAuth::run($config, true, false);
        }else{
            $a = PubAuth::run($config, true, true);
        }
        if($a == false){
        	$this->isAuth = 1; //没有权限
        	//echo '没有权限';exit;
        	$this->showMessage('没有权限');
        }
    }

} 